package com.study.scala.extractor

object ExtractorTest {

  def main(args: Array[String]) {
    println("Apply method : " + apply("Maxsu", "gmail.com"));
    println("Unapply method : " + unapply("Maxsu@gmail.com"));
    println("Unapply method : " + unapply("Maxsu Ali"));

    val map = Map("zsc" -> "zhaosc", "zsd" -> "zhaosd");
    println(map.get("zsc"))
    println(map.get("zcc"))

    val zsc = map.get("zsc")
    val zcc = map.get("zcc")
    println(show(zsc))
    println(show(zcc))
  }

  // The injection method (optional)
  def apply(user: String, domain: String) = {
    user + "@" + domain
  }

  // The extraction method (mandatory)
  def unapply(str: String): Option[(String, String)] = {
    val parts = str split "@"

    if (parts.length == 2) {
      Some(parts(0), parts(1))
    } else {
      None
    }
  }

  def show(x: Option[String]) = x match {
    case Some(s) => s
    case None => "null"
  }

}